<?php
/**
 * Created by PhpStorm.
 * User: 43074
 * Date: 2020/6/4
 * Time: 18:27
 */

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Api\WeChatController;
use App\Http\Controllers\Controller;
use app\Libraries;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Cache;

class AuthorizerController extends WeChatController
{
    /*
    * component_access_token:令牌
    * component_appid:授权方 appid
    * authorizer_appid:授权方 appid
    */
    public function authorizer_information()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" . $this->component_access_token();
        $data = '{
            "component_appid":"' . env('WeChat_AppId') . '" ,
            "authorizer_appid": "' . $authorizer_appid . '"
        }';
        $ret = json_decode($this->Tool->https_post($url, $data));
        return $this->Tool->content(20000, '', $ret);
    }

    /*
        action:add	添加;delete	删除;set覆盖;get获取
     */
    public function server_domain_name()
    {
        $input = Input::only('authorizer_appid', 'domain', 'type');
        $domain = $input['domain'];
        $type = $input['type'];
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $this->authorizer_refresh_token();
        $data = '{
                "action":"' . $type . '",
                "requestdomain":"https://' . $domain . '",
                "wsrequestdomain":"wss://' . $domain . '",
                "uploaddomain":"https://' . $domain . '",
                "downloaddomain":"https://' . $domain . '"
            }';
        $ret = json_decode($this->Tool->https_post($url, $data));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', $ret);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '设置小程序服务器地址失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '设置小程序服务器地址失败', '');
        }
    }

    public function business_domain_name()
    {
        $input = Input::only('authorizer_appid', 'domain', 'type');
        $domain = $input['domain'];
        $type = $input['type'];
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $this->authorizer_refresh_token();
        $data = '{
                "action":"' . $type . '",
                "webviewdomain":"https://' . $domain . '"
            }';
        $ret = json_decode($this->Tool->https_post($url, $data));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', $ret);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '设置小程序业务域名失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '设置小程序业务域名失败', '');
        }
    }

    public function draft_list()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token=" . $this->component_access_token();
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', $ret->draft_list);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取代码草稿列表失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '获取代码草稿列表失败', '');
        }
    }

    /*
    * draft_id:草稿 ID
    */
    public function add_to_template()
    {
        $input = Input::only('authorizer_appid', 'draft_id');
        $authorizer_appid = $input['authorizer_appid'];
        $draft_id = $input['draft_id'];
        $url = "https://api.weixin.qq.com/wxa/addtotemplate?access_token=" . $this->component_access_token();
        $data = '{
                "draft_id": "' . $draft_id . '"
            }';
        $ret = json_decode($this->Tool->https_post($url, $data));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', 'ok');
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '将草稿添加到代码模板库失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '将草稿添加到代码模板库失败', '');
        }
    }

    public function template_list()
    {
        $input = Input::only('authorizer_appid', 'draft_id');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/gettemplatelist?access_token=" . $this->component_access_token();
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', $ret->template_list);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取代码模板列表失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '获取代码模板列表失败', '');
        }
    }

    public function delete_template()
    {
        $input = Input::only('authorizer_appid', 'template_id');
        $authorizer_appid = $input['authorizer_appid'];
        $template_id = $input['template_id'];
        $url = "https://api.weixin.qq.com/wxa/deletetemplate?access_token=" . $this->component_access_token();
        $data = '{
                "template_id": "' . $template_id . '"
            }';
        $ret = json_decode($this->Tool->https_post($url, $data));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', 'ok');
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '删除指定代码模版失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '删除指定代码模版失败', '');
        }
    }


    /*
     * $template_id : 模板ID
     * $ext_json : 小程序配置文件，json格式
     * $user_version : 代码版本号
     * $user_desc : 代码描述
     * */
    public function upload_Code()
    {
        $input = Input::only('authorizer_appid', 'template_id', 'user_version', 'user_desc');
        $authorizer_appid = $input['authorizer_appid'];
        $user_version = $input['user_version'];
        $user_desc = $input['user_desc'];
        $template_id = $input['template_id'];
        $ext_json = json_encode('{"extEnable": true,"extAppid": "' . $authorizer_appid . '","ext":{"appid": "' . env('WeChat_AppId') . '"}}');
        $url = "https://api.weixin.qq.com/wxa/commit?access_token=" . $this->authorizer_refresh_token();
        $data = '{"template_id":"' . $template_id . '","ext_json":' . $ext_json . ',"user_version":"' . $user_version . '","user_desc":"' . $user_desc . '"}';
        $ret = json_decode($this->Tool->https_post($url, $data));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', 'ok');
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, "为授权的小程序帐号上传小程序代码操作失败", json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '为授权的小程序帐号上传小程序代码操作失败', '');
        }
    }

    public function upload_Code_list()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $this->authorizer_refresh_token();
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $ret->page_list;
//            return $this->Tool->content(20000, '',$ret->page_list);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取已上传的代码的页面列表失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '获取已上传的代码的页面列表失败', '');
        }
    }

    public function experience_qrcode()
    {
        $input = Input::only('authorizer_appid', 'path');
        $authorizer_appid = $input['authorizer_appid'];
        $path = $input['path'];
        if ($path) {
            $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=" . $this->authorizer_refresh_token() . "&path=" . urlencode($path);
        } else {
            $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=" . $this->authorizer_refresh_token();
        }
        $ret = json_decode($this->Tool->https_get($url));
        if (isset($ret->errcode)) {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取体验小程序的体验二维码操作失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '获取体验小程序的体验二维码操作失败', '');
        } else {
            return $this->Tool->content(20000, '', $url);
        }
    }

    public function get_category()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $this->authorizer_refresh_token();
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $ret->category_list;
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取授权小程序帐号的可选类目操作失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return 'no';
        }
    }

    /*
     * $tag : 小程序标签，多个标签以空格分开
     * $title : 小程序页面标题，长度不超过32
     * */
    public function submit_audit()
    {
        $input = Input::only('authorizer_appid', 'first_class', 'second_class', 'tag', 'title', 'first_id', 'second_id', 'address');
        $authorizer_appid = $input['authorizer_appid'];
        $first_class = $input['first_class'];
        $second_class = $input['second_class'];
        $tag = $input['tag'];
        $title = $input['title'];
        $first_id = $input['first_id'];
        $second_id = $input['second_id'];
        $address = $input['address'];
//        $first_class = '';
//        $second_class = '';
//        $tag = "女人坊";
//        $title = "女人坊小程序开发";
//        $first_id = 0;
//        $second_id = 0;
//        $address = "pages/index/index";
        $category = $this->get_category();
        if (!empty($category)) {
            $first_class = $category[0]->first_class ? $category[0]->first_class : '';
            $second_class = $category[0]->second_class ? $category[0]->second_class : '';
            $first_id = $category[0]->first_id ? $category[0]->first_id : 0;
            $second_id = $category[0]->second_id ? $category[0]->second_id : 0;
        }
        $getpage = $this->upload_Code_list();
        if (!empty($getpage) && isset($getpage[0])) {
            $address = $getpage[0];
        }
        $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $this->authorizer_refresh_token();
        $data = '{
                "item_list":[{
                    "address":"' . $address . '",
                    "tag":"' . $tag . '",
                    "title":"' . $title . '",
                    "first_class":"' . $first_class . '",
                    "second_class":"' . $second_class . '",
                    "first_id":"' . $first_id . '",
                    "second_id":"' . $second_id . '"
                }]
            }';
        $ret = json_decode($this->Tool->https_post($url, $data));
        if ($ret->errcode == 0) {
            $date = $this->Tool->datas(1, '');
            $array['appid'] = $authorizer_appid;
            $array['auditid'] = $ret->auditid;
            $array['created_at'] = $date;
            $array['updated_at'] = $data;
            $this->Tool->mysql_insert('wxminiprogram_audit', $array);
            return $this->Tool->content(20000, '', 'ok');
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '小程序提交审核操作失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '小程序提交审核操作失败', '');
        }
    }

    public function undocode_audit()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $this->authorizer_refresh_token();
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', 'ok');
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '审核撤回操作失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '审核撤回操作失败', '');
        }
    }

    public function get_latest_auditstatus()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $this->authorizer_refresh_token();
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            $content = array();
            if (isset($ret->auditid)) {
                $content['auditid'] = $ret->auditid;
            }
            if (isset($ret->status)) {
                $content['status'] = $ret->status;
            }
            if (isset($ret->reason)) {
                $content['reason'] = $ret->reason;
            }
            if (isset($ret->errmsg)) {
                $content['errmsg'] = $ret->errmsg;
            }
            return $this->Tool->content(20000, '', $content);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '查询最新一次提交的审核状态失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '查询最新一次提交的审核状态失败', '');
        }
    }

    public function release()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $this->authorizer_refresh_token();
        $data = '{}';
        $ret = json_decode($this->Tool->https_post($url, $data));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', 'ok');
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '发布已通过审核的小程序失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '发布已通过审核的小程序失败', '');
        }
    }

    public function getcategory()
    {
        $input = Input::only('authorizer_appid');
        $authorizer_appid = $input['authorizer_appid'];
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=" . $this->authorizer_refresh_token();
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', $ret->data);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取当前帐号所设置的类目信息失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '获取当前帐号所设置的类目信息失败', '');
        }
    }

    /*
     * $ids = 模板标题 id
     * $start = 用于分页，表示从 start 开始。从 0 开始计数
     * $limit = 用于分页，表示拉取 limit 条记录。最大为 30
     */
    public function get_template_titles()
    {
        $input = Input::only('authorizer_appid', 'ids', 'start', 'limit');
        $authorizer_appid = $input['authorizer_appid'];
        $ids = $input['ids'];//620
        $start = $input['start'];
        $limit = $input['limit'];
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token=" . $this->authorizer_refresh_token() . "&ids=" . $ids . "&start=" . $start . "&limit=" . $limit;
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', $ret->data);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取模板标题列表失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '获取模板标题列表失败', '');
        }
    }

    public function get_templatekey_words()
    {
        $input = Input::only('authorizer_appid', 'tid');
        $authorizer_appid = $input['authorizer_appid'];
        $tid = $input['tid'];//592
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token=" . $this->authorizer_refresh_token() . "&tid=" . $tid;
        $ret = json_decode($this->Tool->https_get($url));
        if ($ret->errcode == 0) {
            return $this->Tool->content(20000, '', $ret->data);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '获取模板标题下的关键词库失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '获取模板标题下的关键词库失败', '');
        }
    }

    /*
     * $tid = 模板标题 id
     * $kidList = 开发者自行组合好的模板关键词列表，关键词顺序可以自由搭配（例如 [3,5,4] 或 [4,5,3]），最多支持5个，最少2个关键词组合
     * $sceneDesc = 服务场景描述，15个字以内
     */
    public function add_template()
    {
        $input = Input::only('authorizer_appid', 'tid', 'kidList', 'sceneDesc');
        $authorizer_appid = $input['authorizer_appid'];
        $tid = $input['tid'];//592
        $kidList = $input['kidList'];
        $sceneDesc = $input['sceneDesc'];
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=" . $this->authorizer_refresh_token();
        $data['tid'] = $tid;
        $data['kidList'] = $kidList;
        $data['sceneDesc'] = $sceneDesc;
        //进行请求
        $ret = json_decode($this->Tool->curl_datas($url, $data));  //封装好的公共函数
        if ($ret->errcode == 0) {
            $content['priTmplId'] = $ret->priTmplId;
            return $this->Tool->content(20000, '', $content);
        } else {
            $this->Tool->Log($authorizer_appid, $ret->errcode, '组合模板并添加到个人模板库失败', json_encode($ret, JSON_UNESCAPED_UNICODE));
            return $this->Tool->content(20001, '组合模板并添加到个人模板库失败', '');
        }
    }
}